import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/_.dart';

///登录页
class SignUp extends StatefulWidget{

  @override
  _SignUp createState() =>_SignUp();
}

class _SignUp extends State<SignUp> with SingleTickerProviderStateMixin{

  // 手机号
  TextEditingController _phoneController = TextEditingController();

  // 密码
  TextEditingController _passwordController = TextEditingController();



  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    _passwordController.dispose();
    _phoneController.dispose();
    super.dispose();

  }

  Widget login(){
    return Padding(
      padding: EdgeInsets.only(left: 23.0,right: 23.0),
      child: Column(
        children: [
          Form(
            child: Column(
              children: [
                SizedBox(
                  height: Screen.setHeight(20),
                ),
                TextFormField(
                  controller: _phoneController,
                  keyboardType: TextInputType.datetime,
                  cursorRadius: Radius.circular(10),
                  decoration: InputDecoration(
                    labelText: '手机号',
                    prefixIcon: Icon(
                      Icons.stay_current_portrait,
                      color: ColorConfig.ThemeColor,
                    ),
                    fillColor: ColorConfig.ThemeColor,
                    labelStyle: TextStyle(color: ColorConfig.ThemeColor),
                    focusedBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: ColorConfig.ThemeColor)
                    )
                  ),
                ),

                TextFormField(
                  maxLength: 20,
                  controller: _passwordController,
                  obscureText: true,
                  decoration: InputDecoration(
                    counterText: '',
                    labelText: "密码",
                    prefixIcon: Icon(
                      Icons.lock,
                      color: ColorConfig.ThemeColor,
                    ),
                    fillColor: ColorConfig.ThemeColor,
                    labelStyle: TextStyle(color: ColorConfig.ThemeColor),
                    focusedBorder: UnderlineInputBorder(
                      //选中时下边框颜色
                      borderSide: BorderSide(color: ColorConfig.ThemeColor),
                    ),
                  ),

                ),
                SizedBox(
                  height: Screen.setHeight(10),
                ),
                Row(
                 children: [
                   Text('忘记密码？',style: TextStyle(fontSize: 14.0,color: Colors.grey[400]),),
                   Expanded(
                     child: Container(),
                   ),
                   Text('验证码登录/注册',style: TextStyle(fontSize: 14.0,color: Colors.grey[400]),)
                 ],
                  
                ),
                SizedBox(
                  height: Screen.setHeight(30),
                ),
                Container(
                  margin: EdgeInsets.only(left: 20, right: 20, ),
                  width: 280,
                  height: 45.0,
                  child:  ElevatedButton(
                    style: ButtonStyle(backgroundColor:
                    MaterialStateProperty.resolveWith((states) {
                      //设置按下时的背景颜色
                      if (states.contains(MaterialState.pressed)) {
                        return ColorConfig.ThemeOtherColor;
                      }
                      //默认不使用背景颜色
                      return ColorConfig.ThemeColor;
                    })),
                    child: Text("登录/注册", style: TextStyle(fontSize: 20)),
                    onPressed: () async{
                      await userLoginAction();
                    },
                  ),
                ),
                SizedBox(
                  height: Screen.setHeight(30),
                ),
                Split(splitData: '其他方式登录',),
                SizedBox(
                  height: Screen.setHeight(20),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Column(
                      children: [
                        Container(
                          margin: EdgeInsets.only(left: 15.0,right: 15.0),
                          alignment: Alignment.center,
                          width: Screen.setWidth(50),
                          height: Screen.setWidth(50),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(25)),
                              border:Border.all(
                                  color: Colors.blue,
                                  width: 1
                              )
                          ),
                          child: IconButton(
                            onPressed: (){

                            },
                            icon: IconFont.qqIcon(
                                size: 20.0,
                                color: Colors.blue
                            ),
                          ),

                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          'QQ',
                          style: TextStyle(
                            color: Colors.grey[400],
                            fontSize: 14.0
                          ),
                        )

                      ],
                    ),
                    Column(
                      children: [
                        Container(
                          margin: EdgeInsets.only(left: 15.0,right: 15.0),
                          alignment: Alignment.center,
                          width: Screen.setWidth(50),
                          height: Screen.setWidth(50),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(25)),
                              border:Border.all(
                                  color: Colors.green,
                                  width: 1
                              )
                          ),
                          child: IconButton(
                            onPressed: (){

                            },
                            icon: IconFont.weiXinIcon(
                                size: 20.0,
                                color: Colors.green
                            ),
                          ),

                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          '微信',
                          style: TextStyle(
                              color: Colors.grey[400],
                              fontSize: 14.0
                          ),
                        )

                      ],
                    ),
                    Column(
                      children: [
                        Container(
                          margin: EdgeInsets.only(left: 10.0,right: 10.0),
                          alignment: Alignment.center,
                          width: Screen.setWidth(50),
                          height: Screen.setWidth(50),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(25)),
                              border:Border.all(
                                  color: Colors.red,
                                  width: 1
                              )
                          ),
                          child: IconButton(
                            onPressed: (){
                            },
                            icon: IconFont.xinLangIcon(
                                size: 20.0,
                                color: Colors.red

                            ),
                          ),

                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          '新浪',
                          style: TextStyle(
                              color: Colors.grey[400],
                              fontSize: 14.0
                          ),
                        )

                      ],
                    )

                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Future<void> userLoginAction() async{
    if(!Tool.isRegExp(_phoneController.text,Type.Phone)){
      BotToast.showText(text: '手机号不正确，请检查!' );
      return;
    }
    if (!Tool.isRegExp(_passwordController.text,Type.PassWord)) {
      BotToast.showText(text: '密码不小于8位，需包含数字和大小写字母!' );
      return;
    }
    String encodePhone = await Tool.encodeString(await Tool.rsaMain(), _phoneController.text);
    String encodePassword = await Tool.encodeString(await Tool.rsaMain(), _passwordController.text);
    var res = await UserService.login(params:{
      'user_Phone':encodePhone,
      'user_PassWord':encodePassword
    });
    if(await Tool.setUserList(res)){
      await SPreferences().setBool('isLogin', true);
      BotToast.showText(text: '登录成功');
      Modular.to.pushReplacementNamed('/index');
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: (){
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        backgroundColor: ColorConfig.WhiteBackColor,
        body:Stack(
               children: <Widget>[
                Positioned(
                  height: Screen.setHeight(180.0),
                  width: Screen.width(context),
                  child: Container(
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            fit: BoxFit.cover,
                            image: AssetImage(
                              Images.libStaticImgSignUp,
                            )
                        )
                    ),
                  ),
                ),
                 Positioned(
                   top: 80.0,
                   child: Container(
                     margin: EdgeInsets.only(left: 20,right: 20),
                     width: Screen.width(context)-40,
                     height: Screen.setHeight(500),
                     decoration: BoxDecoration(
                         color: ColorConfig.WhiteBackColor,
                         borderRadius: BorderRadius.vertical(
                             top: Radius.elliptical(10, 10),
                             bottom: Radius.elliptical(10, 10)
                         ),
                         boxShadow: [
                           BoxShadow(
                               color: ColorConfig.ThemeOtherColor,
                               offset: Offset(0,0),
                               blurRadius: 5,
                               spreadRadius: 2
                           )
                         ]
                     ),
                     child: Column(
                       crossAxisAlignment: CrossAxisAlignment.start,
                       children: [
                         Container(
                           margin: EdgeInsets.only(top: 20.0,left: 30.0),
                           height: Screen.setHeight(35),
                           child: Text(
                             '登录',
                             style: TextStyle(
                                 color: ColorConfig.ThemeColor,
                                 fontWeight: FontWeight.w400,
                                 fontSize: 20
                             ),
                           ),

                         ),
                         Container(
                             height:  Screen.setHeight(500) - Screen.setHeight(35) - 30,
                             margin: EdgeInsets.only(top: 10),
                             child: login()
                         )
                       ],
                     ),
                   ),
                 ),
                 Container(
                   margin: EdgeInsets.only(top: 560),
                   alignment: Alignment.center,
                   width: Screen.width(context),
                   child: RichText(
                     text: TextSpan(
                         style: TextStyle(fontSize: 13.0,color: Colors.grey[400]),
                         children: <TextSpan>[
                           TextSpan(
                               text: '登录即代表同意并阅读'
                           ),
                           TextSpan(
                             text: '《服务协议》',
                             style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
                             recognizer: TapGestureRecognizer()..onTap = () {
                             },
                           )
                         ]
                     ),
                   ),
                 ),
              ],
            ),


        )

      );
  }
}